//
//  CollectViewController.m
//  FlowerRainGround
//
//  Created by 阿仁欧巴 on 16/4/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "CollectViewController.h"
#import "FirstModel.h"
#import "XMGWaterflowLayout.h"
#import "CollectionDB.h"
#import "CollectionViewCell.h"

static NSString *const reuse = @"cell";

@interface CollectViewController ()<UICollectionViewDataSource, XMGWaterflowLayoutDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataArray;//瀑布流的数组
@property (nonatomic, strong) NSArray *collectArray;//收藏的数据数组
@property (nonatomic, strong) NSMutableArray *testArray; //测试数据

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation CollectViewController

- (NSMutableArray *)testArray{
    if (_testArray == nil) {
        _testArray = [NSMutableArray array];
    }
    return _testArray;
}


- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
//查询数据
- (void)findDB
{
    CollectionDB *db = [[CollectionDB alloc] init];
    self.collectArray = [db findModelWithUserID:[userManager getUsrID]];
    //刷新列表
    [self.collectionView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    [self findDB];

    [self setupLayout];
    [self setupRefresh];

    
    // Do any additional setup after loading the view.
}


//创建布局
- (void)setupLayout
{
    XMGWaterflowLayout *layout = [[XMGWaterflowLayout alloc] init];
    layout.delegate = self;
    //创建collectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) collectionViewLayout:layout];
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
    //注册
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:reuse];
    self.collectionView = collectionView;
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
//    self.collectionView.delegate = self;
}

//刷新

- (void)setupRefresh
{
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    [self.collectionView.header beginRefreshing];
    
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.collectionView.footer.hidden = YES;


}

- (void)loadNewData
{
    //在一秒之后刷新数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新之前先清除所有数据
//        [self.dataArray removeAllObjects];
//        [self.dataArray addObjectsFromArray:self.collectArray];
        NSArray *array = [ShopModel objectArrayWithFilename:@"1.plist"];
        NSLog(@"%@",array);
//        [self.dataArray removeAllObjects];
        [self.testArray removeAllObjects];
#warning mark ---这里是将数据源的数组中的模型添加到数据数组中 ---
        [self.testArray addObjectsFromArray:array];

        
        //刷新数据
        [self.collectionView reloadData];
        [self.collectionView.header endRefreshing];
    });

}

- (void)loadMoreShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *array = [ShopModel objectArrayWithFilename:@"1.plist"];
        
#warning mark ---这里是将数据源的数组中的模型添加到数据数组中 ---
        [self.testArray addObjectsFromArray:array];
        
        
        [self.collectionView reloadData];
        [self.collectionView.footer endRefreshing];
    });
    
 
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

        self.collectionView.footer.hidden = self.testArray.count == 0;
//    return self.dataArray.count;
   return self.testArray.count;
//    return 50;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    
    cell.model = self.testArray[indexPath.item];
//    cell.headImageView.image = [UIImage imageNamed:@"0"];
    
    return cell;
}

#pragma mark - <XMGWaterflowLayoutDelegate>

#warning mark ---根据模型 设置瀑布流的宽高

- (CGFloat)waterflowLayout:(XMGWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth
{
    ShopModel *model = self.testArray[index];
    return itemWidth * model.h / model.w;
//    return arc4random() %61 + 60;
}

- (CGFloat)rowMarginInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout
{
    return 10;
}

- (CGFloat)columnCountInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout
{
    if (self.testArray.count <= 50) return 2;
    return 3;
}

- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(XMGWaterflowLayout *)waterflowLayout
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
